package com.javajun.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.custom.JqGridParm;
import com.javajun.custom.JqGridUtils;
import com.javajun.entity.Empower;
import com.javajun.entity.Enterprise;
import com.javajun.entity.SysAdmin;
import com.javajun.entity.UserRole;
import com.javajun.form.JEnterpriseForm;
import com.javajun.form.JSysAdminForm;
import com.javajun.service.SysAdminService;
import com.javajun.service.UserRoleService;

@Controller
@RequestMapping(value="/sysAdmin")
public class SysAdminController {

    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    private UserRoleService userRoleService;
    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    
    @RequestMapping(value="/manage", method=RequestMethod.GET)
    public String manage() {
        return "sysAdmin/manage";
    }
    
    @RequestMapping(value = "getlist", method=RequestMethod.POST)
    // 返回json数据
    @ResponseBody
    public Object getList(JqGridParm parm) {
        Pageable pageable = parm.buildPageable();
        Page<SysAdmin> page = null;
        if (!parm.getSearch()) {
            page = sysAdminService.findAll(pageable);
        } else {
            page = sysAdminService.findAll(parm.buildSpec(), pageable);
        }
        HashMap<String, Object> pageResult = JqGridUtils.getPageResult(page);
        return pageResult;
    }
    
    @RequestMapping(value = { "/add", "/edit" }, method = RequestMethod.POST)
    @ResponseBody
    public Object addAndEdit(ModelMap map, String[] id, JSysAdminForm form) {
        // DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");// 将时间格式化 
        // BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        SysAdmin entity = new SysAdmin();
        String mess = "";
        if (form.getOper().equals("add")) {
            Integer sysAdminId = null;
            save(sysAdminId, form);
            mess = "Add is OK!";
        } else if (form.getOper().equals("edit")) {
            Integer sysAdminId = null;
            for (int i = 0; i < id.length; i++) {
                System.out.println(id[i]);
                sysAdminId = Integer.parseInt(id[i]);
            }
            save(sysAdminId, form);
        } else if (form.getOper().equals("del")) {
            for (int i = 0; i < id.length; i++) {
                Integer sysAdminId = Integer.parseInt(id[i]);
                delete(map, sysAdminId);
                System.out.println("删除用户id：" + id[i]);
            }
            mess = "Delete is OK!";
        }
        return mess;
    }

    private void save(Integer id, JSysAdminForm form) {
        SysAdmin entity = new SysAdmin();
        if (id != null) {
            entity = sysAdminService.findById(id);
        }
        if (id == null) {
            entity.setPassword(encoder.encode("1111"));
        }
        if (form.getUsername() != null) {
            entity.setUsername(form.getUsername());
        }
        if (form.getPassword() != null) {
            entity.setPassword(form.getPassword());
        }
        if (form.getName() != null) {
            entity.setName(form.getName());
        }
        // 密码
        if (entity.getRoles() == null) {
            List<UserRole> roles = new ArrayList<>();
            roles.add(userRoleService.findByCode("ROLE_ADMIN"));
            entity.setRoles(roles);
        }
        sysAdminService.save(entity);
    }

    /**
     * Delete 通过Id删除一条记录
     * @param map
     * @param id
     */
    private void delete(ModelMap map, Integer id) {
        
        if (id != null) {
            sysAdminService.deleteById(id);
        }
    }
}
